﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageSkutable.Queries
{
    public class GetSkutableNameByUserIdQueries : IRequest<Json<List<GetSkutableNameByUserIdDTO>>>
    {

    }

    public class GetSkutableNameByUserIdQueriesHandler : IRequestHandler<GetSkutableNameByUserIdQueries, Json<List<GetSkutableNameByUserIdDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IskutableRepository _iskutableRepository;
        public GetSkutableNameByUserIdQueriesHandler(ICurrentUserService icurrentUserServices, IskutableRepository iskutableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iskutableRepository = iskutableRepository;
        }
        public async Task<Json<List<GetSkutableNameByUserIdDTO>>> Handle(GetSkutableNameByUserIdQueries request, CancellationToken cancellationToken)
        {
            List<GetSkutableNameByUserIdDTO> list = new List<GetSkutableNameByUserIdDTO>() { };
            int userid = _icurrentUserServices.GetUserId();
            if (userid <= 0)
            {
                return new Json<List<GetSkutableNameByUserIdDTO>>() { Status = 1003, Detail = "请先登录", Data = list };
            }
            else {
                list = await _iskutableRepository.GetSkutableNameListByUserId(userid, cancellationToken);
                if (!(list != null && list.Any()))
                {
                    list = new List<GetSkutableNameByUserIdDTO>() { };
                }
                return new Json<List<GetSkutableNameByUserIdDTO>>() { Status = 1000, Detail = "操作成功", Data = list };
            }

        }
    }
}
